

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="若发现文章有误，敬请指出，感谢@[toc]  一、运行环境  Vmware CentOS 7 操作系统 JDK 8 MySQL8 Hadoop3.3.0（单节点） HIve 3.1.2 on YARN Maven 3.8.4 IDEA 2021.3 旗舰版  节点分配： 其中MySQL和Hive都安装在hadoop101节点 ![在这里插入图片描述](https:&#x2F;&#x2F;img-blog.csdni">
<meta property="og:type" content="article">
<meta property="og:title" content="Hive 笔记3 使用Java实现自定义SQL函数 _ Hive三大类型函数的简要区分 UDF、UDAF、UDTF">
<meta property="og:url" content="http://example.com/2022/03/26/Hive%20%E7%AC%94%E8%AE%B03%20%E4%BD%BF%E7%94%A8Java%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89SQL%E5%87%BD%E6%95%B0%20_%20Hive%E4%B8%89%E5%A4%A7%E7%B1%BB%E5%9E%8B%E5%87%BD%E6%95%B0%E7%9A%84%E7%AE%80%E8%A6%81%E5%8C%BA%E5%88%86%20UDF%E3%80%81UDAF%E3%80%81UDTF/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="若发现文章有误，敬请指出，感谢@[toc]  一、运行环境  Vmware CentOS 7 操作系统 JDK 8 MySQL8 Hadoop3.3.0（单节点） HIve 3.1.2 on YARN Maven 3.8.4 IDEA 2021.3 旗舰版  节点分配： 其中MySQL和Hive都安装在hadoop101节点 ![在这里插入图片描述](https:&#x2F;&#x2F;img-blog.csdni">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-26T06:11:56.000Z">
<meta property="article:modified_time" content="2022-08-22T15:44:47.634Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Hive">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>Hive 笔记3 使用Java实现自定义SQL函数 _ Hive三大类型函数的简要区分 UDF、UDAF、UDTF - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Hive 笔记3 使用Java实现自定义SQL函数 _ Hive三大类型函数的简要区分 UDF、UDAF、UDTF"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-03-26 14:11" pubdate>
          2022年3月26日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          6.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          51 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Hive 笔记3 使用Java实现自定义SQL函数 _ Hive三大类型函数的简要区分 UDF、UDAF、UDTF</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <blockquote>
<p>若发现文章有误，敬请指出，感谢<br>@[toc]</p>
</blockquote>
<h1 id="一、运行环境"><a href="#一、运行环境" class="headerlink" title="一、运行环境"></a>一、运行环境</h1><hr>
<ul>
<li>Vmware</li>
<li>CentOS 7 操作系统</li>
<li>JDK 8</li>
<li>MySQL8</li>
<li>Hadoop3.3.0（单节点）</li>
<li>HIve 3.1.2 on YARN</li>
<li>Maven 3.8.4</li>
<li>IDEA 2021.3 旗舰版</li>
</ul>
<p>节点分配：</p>
<p>其中MySQL和Hive都安装在hadoop101节点</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/61b29cb13cdf49a3a75125c745ff6a27.png#">https://img-blog.csdnimg.cn/61b29cb13cdf49a3a75125c745ff6a27.png#</a> &#x3D;50%x)</p>
<h1 id="二、Hive的内置函数"><a href="#二、Hive的内置函数" class="headerlink" title="二、Hive的内置函数"></a>二、Hive的内置函数</h1><hr>
<p>官方参考文档：<a target="_blank" rel="noopener" href="https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-Built-inFunctions">点击查看</a><br>Hive提供的函数主要分为三种，分别是 <strong>UDF、UDAF、UDTF</strong>，它们之间的比较如下表：</p>
<table>
<thead>
<tr>
<th align="left">函数类型</th>
<th align="left">全称</th>
<th align="left">描述</th>
<th align="left">范例</th>
</tr>
</thead>
<tbody><tr>
<td align="left">UDF</td>
<td align="left">User Defined Function</td>
<td align="left">一进一出</td>
<td align="left">floor()向下取整、ceil()向上取整…</td>
</tr>
<tr>
<td align="left">UDAF</td>
<td align="left">User Defined Aggregation Function</td>
<td align="left">多进一出</td>
<td align="left">min(col)取最小值、max(col)取最大值…</td>
</tr>
<tr>
<td align="left">UDTF</td>
<td align="left">User Defined Table-Generating Functions</td>
<td align="left">一进多出</td>
<td align="left">explode()炸裂函数，可遍历一个集合、数组等</td>
</tr>
</tbody></table>
<p>如何自定义UDF、UDTF函数？（由于官方提供的UDAF函数十分丰富，所以一般不需要自定义）<br><strong>步骤如下：</strong></p>
<ol>
<li>继承Hive提供的类<br> org.apache.hadoop.hive.ql.udf.generic.GenericUDF;<br> org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;</li>
<li>实现要求实现的方法</li>
<li>打包成jar包提交到Hive所在的集群节点（如果在Hive根目录的lib文件夹下就无需手动导入，否则需要在Hive Shell里手动执行<code>add jar 文件位置</code>，添加jar包</li>
<li>注册函数，使用 <code>create temporary function 自定义函数名 as &quot;实现类的包名加上类名&quot;;</code> 命令来完成自定义函数的注册</li>
<li>最后测试函数，可以使用实际表的数据，也可以直接用字符串或者变量来进行测试。</li>
</ol>
<h1 id="三、自定义UDF函数"><a href="#三、自定义UDF函数" class="headerlink" title="三、自定义UDF函数"></a>三、自定义UDF函数</h1><hr>
<p>官方参考文档：<a target="_blank" rel="noopener" href="https://cwiki.apache.org/confluence/display/Hive/HivePlugins">点击查看</a></p>
<h2 id="3-1-编写代码实现自定义的Hive函数"><a href="#3-1-编写代码实现自定义的Hive函数" class="headerlink" title="3.1  编写代码实现自定义的Hive函数"></a>3.1  编写代码实现自定义的Hive函数</h2><p><code>pom.xml</code> Maven 依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.uni<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>project<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">packaging</span>&gt;</span>pom<span class="hljs-tag">&lt;/<span class="hljs-name">packaging</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modules</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">module</span>&gt;</span>hive-demo<span class="hljs-tag">&lt;/<span class="hljs-name">module</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">modules</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.source</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.source</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.target</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.target</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 声明并引入，所有子模块都会自动引入该依赖 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- https://mvnrepository.com/artifact/org.apache.hive/hive-common --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.hive<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>hive-exec<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.1.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>myUDF.java</code> 实现代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.hive;<br><br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.ql.exec.UDFArgumentException;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.ql.metadata.HiveException;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.ql.udf.generic.GenericUDF;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * Author: Unirithe</span><br><span class="hljs-comment"> * Date: 2022/3/26</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">myUDF</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">GenericUDF</span> &#123;<br><br>    <span class="hljs-comment">// 校验数据参数个数</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> ObjectInspector <span class="hljs-title function_">initialize</span><span class="hljs-params">(ObjectInspector[] objectInspectors)</span> <span class="hljs-keyword">throws</span> UDFArgumentException &#123;<br>        <span class="hljs-keyword">if</span>(objectInspectors.length != <span class="hljs-number">1</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">UDFArgumentException</span>(<span class="hljs-string">&quot;参数个数不为1&quot;</span>);<br>        <span class="hljs-keyword">return</span> PrimitiveObjectInspectorFactory.javaIntObjectInspector;<br>    &#125;<br><br>    <span class="hljs-comment">// 处理数据</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">evaluate</span><span class="hljs-params">(DeferredObject[] deferredObjects)</span> <span class="hljs-keyword">throws</span> HiveException &#123;<br>        <span class="hljs-comment">// 1. 取出输入数据</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">input</span> <span class="hljs-operator">=</span> deferredObjects[<span class="hljs-number">0</span>].get().toString();<br>        <span class="hljs-comment">// 2. 判断输入数据是否为NULL</span><br>        <span class="hljs-keyword">if</span>(input == <span class="hljs-literal">null</span>)<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">else</span><br>            <span class="hljs-keyword">return</span> input.length();<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getDisplayString</span><span class="hljs-params">(String[] strings)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;&quot;</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="3-2-打包成jar包，发布到集群节点，进行测试"><a href="#3-2-打包成jar包，发布到集群节点，进行测试" class="headerlink" title="3.2  打包成jar包，发布到集群节点，进行测试"></a>3.2  打包成jar包，发布到集群节点，进行测试</h2><p>使用Maven进行打包，将jar包发布到Hive所在的集群节点Hive根目录下的lib文件夹里</p>
<p>若放在其他目录，则需在Hive Shell里手动导入jar包</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql">hive <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">add</span> jar <span class="hljs-operator">/</span>opt<span class="hljs-operator">/</span><span class="hljs-keyword">module</span><span class="hljs-operator">/</span>hive<span class="hljs-operator">/</span>data<span class="hljs-operator">/</span>hive<span class="hljs-operator">-</span>demo<span class="hljs-number">-1.0</span><span class="hljs-operator">-</span>SNAPSHOT.jar; <br></code></pre></td></tr></table></figure>

<p>创建临时函数，将函数与自定义的类进行关联</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql">hive <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">create</span> temporary <span class="hljs-keyword">function</span> my_len <span class="hljs-keyword">as</span> &quot;com.uni.hive.myUDF&quot;;<br></code></pre></td></tr></table></figure>

<p>然后随便使用一张表（至少要有一行数据）进行测试</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/2ada4d3b02314bb4bece1658fb23d5b4.png#">https://img-blog.csdnimg.cn/2ada4d3b02314bb4bece1658fb23d5b4.png#</a> &#x3D;80%x)</p>
<h1 id="四、自定义UDTF函数"><a href="#四、自定义UDTF函数" class="headerlink" title="四、自定义UDTF函数"></a>四、自定义UDTF函数</h1><hr>
<h2 id="4-1-编写代码"><a href="#4-1-编写代码" class="headerlink" title="4.1  编写代码"></a>4.1  编写代码</h2><p><code>pom.xml</code> 引入Maven依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.hive<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>hive-exec<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.1.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><code>myUDTF.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.hive;<br><br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.ql.exec.UDFArgumentException;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.ql.metadata.HiveException;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;<br><span class="hljs-keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 输入数据: hello,uni,hive</span><br><span class="hljs-comment"> * 输出数据：</span><br><span class="hljs-comment"> *          hello</span><br><span class="hljs-comment"> *          uni</span><br><span class="hljs-comment"> *          hive</span><br><span class="hljs-comment"> * Author: Unirithe</span><br><span class="hljs-comment"> * Date: 2022/3/26</span><br><span class="hljs-comment"> **/</span><br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">myUDTF</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">GenericUDTF</span> &#123;<br><br>    <span class="hljs-keyword">private</span> ArrayList&lt;String&gt; outList = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> StructObjectInspector <span class="hljs-title function_">initialize</span><span class="hljs-params">(StructObjectInspector argOIs)</span> <span class="hljs-keyword">throws</span> UDFArgumentException &#123;<br><br>        <span class="hljs-comment">// 输出数据的默认列名，可以被别名覆盖</span><br>        List&lt;String&gt; fieldNames = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-comment">// 输出数据的类型</span><br>        List&lt;ObjectInspector&gt; fieldOIs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-comment">// 设置别名</span><br>        fieldNames.add(<span class="hljs-string">&quot;TheWord&quot;</span>);<br>        fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);<br>        <span class="hljs-comment">// 最终返回值</span><br>        <span class="hljs-keyword">return</span> ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);<br>    &#125;<br><br>    <span class="hljs-comment">// 处理输入数据 hello,uni,hive</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">process</span><span class="hljs-params">(Object[] args)</span> <span class="hljs-keyword">throws</span> HiveException &#123;<br>        <span class="hljs-comment">// 1. 取出输入数据</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">input</span> <span class="hljs-operator">=</span> args[<span class="hljs-number">0</span>].toString();<br>        <span class="hljs-comment">// 2. 获取分隔符</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">splitKey</span> <span class="hljs-operator">=</span> args[<span class="hljs-number">1</span>].toString();<br>        <span class="hljs-comment">// 2. 按照 &quot;,&quot; 分割字符串</span><br>        String[] fields = input.split(splitKey);<br>        <span class="hljs-comment">// 3.遍历数据写出</span><br>        <span class="hljs-keyword">for</span> (String field : fields) &#123;<br>            <span class="hljs-comment">// 清空集合</span><br>            outList.clear();<br>            <span class="hljs-comment">// 将数据放入集合</span><br>            outList.add(field);<br>            <span class="hljs-comment">// 输出数据</span><br>            forward(outList);<br>        &#125;<br><br>    &#125;<br>    <span class="hljs-comment">// 收尾方法</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">close</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> HiveException &#123;<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="4-2-测试"><a href="#4-2-测试" class="headerlink" title="4.2  测试"></a>4.2  测试</h2><p>将上面的程序打包后发到Hive所在的集群节点，移动到hive根目录下的lib文件夹即可</p>
<p>插入测试数据</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql">hive <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> testudtf(words string);<br>hive <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> testudtf <span class="hljs-keyword">values</span>(<span class="hljs-string">&#x27;hello,uni,hive&#x27;</span>), (<span class="hljs-string">&#x27;hi,hadoop&#x27;</span>);<br>hive <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">select</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">from</span> testudtf;<br></code></pre></td></tr></table></figure>
<p>创建函数</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql">hive <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">create</span> temporary <span class="hljs-keyword">function</span> my_explode <span class="hljs-keyword">as</span> &quot;com.uni.hive.myUDTF&quot;;<br></code></pre></td></tr></table></figure>
<p>测试程序效果</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql">hive <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">select</span> my_explode(words, <span class="hljs-string">&#x27;,&#x27;</span>) <span class="hljs-keyword">from</span> testudtf;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs ebnf"><span class="hljs-attribute">theword</span><br><span class="hljs-attribute">hello</span><br><span class="hljs-attribute">uni</span><br><span class="hljs-attribute">hive</span><br><span class="hljs-attribute">hi</span><br><span class="hljs-attribute">hadoop</span><br></code></pre></td></tr></table></figure>
<p>除了使用结构表测试，还可以直接使用字符串测试</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql">hive <span class="hljs-operator">&gt;</span> <span class="hljs-keyword">select</span> my_explode(<span class="hljs-string">&#x27;hi-hadoop-do-you-like-sleep&#x27;</span>, <span class="hljs-string">&#x27;-&#x27;</span>);<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong></p>
<figure class="highlight vim"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs vim">theword<br><span class="hljs-keyword">hi</span><br>hadoop<br><span class="hljs-keyword">do</span><br>you<br>like<br><span class="hljs-keyword">sleep</span><br></code></pre></td></tr></table></figure>

<h1 id="参-考-资-料"><a href="#参-考-资-料" class="headerlink" title="参  考  资  料"></a>参  考  资  料</h1><hr>
<p>[1]  <a target="_blank" rel="noopener" href="https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-Built-inFunctions">Hive官方文档</a><br>[2] <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1EZ4y1G7iL?p=87">视频链接</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93/" class="category-chain-item">数据仓库</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Hive/">#Hive</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Hive 笔记3 使用Java实现自定义SQL函数 _ Hive三大类型函数的简要区分 UDF、UDAF、UDTF</div>
      <div>http://example.com/2022/03/26/Hive 笔记3 使用Java实现自定义SQL函数 _ Hive三大类型函数的简要区分 UDF、UDAF、UDTF/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年3月26日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/03/29/Git%20%E7%AC%94%E8%AE%B0_%20Git%E5%88%86%E5%B8%83%E5%BC%8F%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E5%B7%A5%E5%85%B7%20%E6%A6%82%E8%BF%B0%20_%20%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%20_%20%E5%88%86%E6%94%AF%E6%93%8D%E4%BD%9C%20_%20%E4%BD%BF%E7%94%A8%20IDEA%20%E8%BF%9E%E6%8E%A5Gitee%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%20+Demo/" title="Git 笔记_ Git分布式版本控制工具 概述 _ 常用命令 _ 分支操作 _ 使用 IDEA 连接Gitee远程仓库">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Git 笔记_ Git分布式版本控制工具 概述 _ 常用命令 _ 分支操作 _ 使用 IDEA 连接Gitee远程仓库</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/03/25/Hive%20%14%EF%BF%BD2%20%7F(%EF%BF%BD%EF%BF%BD%EF%BF%BDpover%EF%BF%BD%EF%BF%BD5F8%EF%BF%BD%EF%BF%BD*%00B_%20%EF%BF%BD%EF%BF%BD%EF%BF%BD*%EF%BF%BD%1F-p%C7%84~%EF%BF%BD%EF%BF%BD%EF%BF%BD;%EF%BF%BDp_%20~%EF%BF%BD-p%0E%EF%BF%BD%EF%BF%BD%EF%BF%BD;%EF%BF%BD%20_%20%09g%EF%BF%BD%1F%EF%BF%BD%EF%BF%BD9%EF%BF%BDL/%EF%BF%BD%20_%EF%BF%BD%EF%BF%BD~%EF%BF%BD%0A!-p%EF%BF%BD%EF%BF%BD%20_%20%EF%BF%BD%EF%BF%BDM20%25%EF%BF%BD%EF%BF%BD/" title="Hive 笔记2 使用窗口函数over实现电商常见五个需求_ 查询某个时期购买过的顾客以及总人数_ 顾客购买明细以及总额 _ 按照日期对花费进行累加 _查询顾客上次购买时间 _ 查询前20%时间">
                        <span class="hidden-mobile">Hive 笔记2 使用窗口函数over实现电商常见五个需求_ 查询某个时期购买过的顾客以及总人数_ 顾客购买明细以及总额 _ 按照日期对花费进行累加 _查询顾客上次购买时间 _ 查询前20%时间</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
